﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public partial class UserControls_UserImgSelector : UserImgSelectorCtlBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public bool ImageOnlyMode
    {
        get;
        set;
    }
    public string ImageTypes { get; set; }
    public bool FixImgAndWaterMark { get; set; }

    public string Value
    {
        get { return TxtVal.Value; }
        set
        {
            TxtVal.Value = value;
            PV.NavigateUrl = value;
            CheckBox1.Enabled = !string.IsNullOrEmpty(value);
        }
    }
    public bool ReadOnly
    {
        get
        {
            object v = ViewState["ReadOnly"];
            if (null != v)
            {
                return (bool)v;
            }
            else
            {
                return default(bool);
            }
        }
        set
        {
            ViewState["ReadOnly"] = value;
            FileUpload1.Visible = CheckBox1.Visible= !value;

        }
    }

    //public string SelText
    //{
    //    get
    //    {
    //        return SelButton.Text;
    //    }
    //    set {
    //        SelButton.Text = value;
    //    }
    //}

    public override bool SaveFile() {
        if (ReadOnly) return true;
        LastVal = Value;

        if (CheckBox1.Checked) {
            ClearVal();
            CheckBox1.Checked = false;
            return true;
        }

        if (FileUpload1.HasFile)
        {
            ClearVal();
            string extension = System.IO.Path.GetExtension(FileUpload1.FileName);
            string nfn = "~/UserFiles/UserUpload/" + xIx.Data.DbHelper.StaticGenUid().ToString() + extension;
            string fn = Server.MapPath(nfn);
            FileUpload1.SaveAs(fn);
            if (extension.ToLower() == "png" || extension.ToLower() == "jpg" || extension.ToLower() == "bmp" || extension.ToLower() == "jpeg")
            {
                string waterImage = Server.MapPath("~/UserFiles/Image/WaterImage.jpg");
                CreateImageWaterMark(fn, waterImage, fn);
            }
            System.IO.File.WriteAllLines(fn + ".fileinfo", new string[] { System.IO.Path.GetFileName(FileUpload1.FileName) }, System.Text.Encoding.UTF8);
            System.IO.File.SetAttributes(fn + ".fileinfo", System.IO.FileAttributes.Hidden);

            Value = nfn;
            return true;
        }
        else {
            return true;
        }
    }

    /// <summary>
    /// 对一个指定的图片加上图片水印效果。
    /// </summary>
    /// <param name="imageFile">图片文件地址</param>
    /// <param name="waterImage">水印图片（Image对象）</param>
    public static void CreateImageWaterMark(string imageFile, string waterFile, string newPath)
    {
        System.Drawing.Image waterImage = System.Drawing.Image.FromFile(waterFile);
        if (string.IsNullOrEmpty(imageFile) || !File.Exists(imageFile) || waterImage == null)
        {
            return;
        }

        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);
        int x = waterImage.Width + 20;//(int)(waterImage.Width * 0.87 + waterImage.Height * 0.5);//originalImage.Width - waterImage.Width - 10;
        int y = waterImage.Height + 20;//(int)(waterImage.Width * 0.5 + waterImage.Height * 0.87);//originalImage.Height - waterImage.Height - 10;
        int width = waterImage.Width;
        int height = waterImage.Height;
        int xtemp = 0;
        int ytemp = 0;
        while (originalImage.Height > ytemp)
        {
            while (originalImage.Width > xtemp)
            {
                Graphics graphics = Graphics.FromImage(originalImage);

                ImageAttributes imageAttributes = new ImageAttributes();
                ColorMap colorMap = new ColorMap();

                colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
                colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
                ColorMap[] remapTable = { colorMap };

                imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

                float[][] colorMatrixElements = {
　　　　　　　　　　　　 new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
　　　　　　　　　　　　 new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
　　　　　　　　　　　　 new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
　　　　　　　　　　　　 new float[] {0.0f, 0.0f, 0.0f, 0.2f, 0.0f},
　　　　　　　　　　　　 new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
　　　　　　　　　　　 };
                ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
                imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                graphics.DrawImage(waterImage, new Rectangle(xtemp, ytemp, width, height), 0, 0, width, height, GraphicsUnit.Pixel, imageAttributes);
                xtemp += x;
                graphics.Dispose();
            }
            xtemp = 0;
            ytemp += y;
        }
        MemoryStream stream = new MemoryStream();
        originalImage.Save(stream, ImageFormat.Jpeg);
        originalImage.Dispose();
        System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);

        imageWithWater.Save(newPath);
        imageWithWater.Dispose();
    }

    public string LastVal
    {
        get
        {
            object v = ViewState["LastVal"];
            if (null != v)
            {
                return (string)v;
            }
            else
            {
                return default(string);
            }
        }
        set
        {
            ViewState["LastVal"] = value;
        }
    }
    public override bool DelFile()
    {
        if (ReadOnly) return true;
        ClearVal();
        Value = LastVal;
        return true;
    }


    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        ClearVal();

    }

    private void ClearVal()
    {
        if (!string.IsNullOrEmpty(Value))
        {
            try
            {
                System.IO.File.Delete(Server.MapPath(Value));
                System.IO.File.Delete(Server.MapPath(Value + ".fileinfo"));
            }
            catch { }
            Value = string.Empty;
        }
    }

    #region ITextControl 成员

    public override string Text
    {
        get
        {
            return Value;
        }
        set
        {
            Value = value;
        }
    }

    #endregion
}
